<%@ include file="../include/taglib.jsp" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<!-- Modal -->
<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h4 class="modal-title">
        修改密码
    </h4>
</div>
<div class="modal-body">
    <form id="editForm" class="form-horizontal style-form">
        <input type="hidden" value="${loginUser.id}" name="id"/>
        <div class="form-group">
            <label class="col-sm-2 control-label">原密码</label>
            <div class="col-sm-6">
                <input type="password" class="form-control" name="encryptedPassword" placeholder="6~10位字母数字特殊字符组合..."/>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-2 control-label">新密码</label>
            <div class="col-sm-6">
                <input type="password" class="form-control" name="plainPassword" placeholder="6~10位字母数字特殊字符组合..."/>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-2 control-label">确认新密码</label>
            <div class="col-sm-6">
                <input type="password" class="form-control" name="confirmPassword" placeholder="6~10位字母数字特殊字符组合..."/>
            </div>
        </div>

    </form>
</div>
<div class="modal-footer">
    <button data-dismiss="modal" class="btn btn-default" type="button" id="cancelBtn">取消</button>
    <button class="btn btn-theme" type="button" id="saveBtn">保存</button>
</div>
<!-- modal -->


<script type="text/javascript">
    $(function () {
        $("#saveBtn").bind("click", function () {
            if (!checkForm("#editForm")) {
                return;
            }
            var data = $("#editForm").serializeArray();
            var package = new AJAXPacket("${ctx}/sysuser/chgpwd");
            package.data.data = data;
            core.ajax.sendPacket(package, function (response) {
                if (response.data.success) {
                    alertMsg.correct("成功！");
                    $("#cancelBtn").click();
                } else {
                    alertMsg.error(response.data.errMsg);
                }
            });
            package = null;
        });
    });

    function checkForm(obj) {
        var encryptedPassword = $(obj).find("input[name=encryptedPassword]").val();
        if (encryptedPassword == null || encryptedPassword == "") {
            alertMsg.warn("原密码不能为空！");
            return false;
        }
        var plainPassword = $(obj).find("input[name=plainPassword]").val();
        if (plainPassword == null || plainPassword == "") {
            alertMsg.warn("新密码不能为空！");
            return false;
        }
        var patten = new RegExp(/^.{6,12}$/);
        if (!patten.test(plainPassword)) {
            alertMsg.warn("新密码格式不正确，请输入6~12位字符的组合！");
            $(obj).find("input[name=plainPassword]").focus();
            return false;
        }
        if (!isPassword(plainPassword)) {
            alertMsg.warn("新密码格式不正确，请输入字母，数字，特殊字符的组合！");
            $(obj).find("input[name=plainPassword]").focus();
            return false;
        }
        var confirmPassword = $(obj).find("input[name=confirmPassword]").val();
        if (plainPassword != confirmPassword) {
            alertMsg.warn("两次输入的密码不一致！");
            $(obj).find("input[name=confirmPassword]").focus();
            return false;
        }
        return true;
    }

    /**
     * 校验密码强度
     * 强：字母+数字+特殊字符
     * @param s
     * @returns {boolean}
     */
    function isPassword(s) {
        var pattern = /^(?![a-zA-z]+$)(?!\d+$)(?![!@#$%^&*]+$)(?![a-zA-z\d]+$)(?![a-zA-z!@#$%^&*]+$)(?![\d!@#$%^&*]+$)[a-zA-Z\d!@#$%^&*]+$/;
        if (!pattern.exec(s)) {
            return false;
        }
        return true;
    }
</script>